import utils from './utils'
import consts from "@/utils/consts";
import auth from "@/utils/auth";
export default {
    fileType:fileType,
    demand:demand,
    job:job,
    chat:chat,
    bug: bug,
};


function fileType(url) {
    if (!url) {return ""}
    let ary = url.split("/")
    if (!ary || ary.length === 0) {return ""}
    
    let suffix = ""
    if (ary && ary.length > 0) {
        suffix = ary[ary.length - 1]
        ary = suffix.split(".")
        if (ary && ary.length > 0) {
            suffix = ary[ary.length - 1]
        }
    }
    suffix = suffix.toLowerCase()
    switch (suffix) {
        case "docx":
        case "docm":
        case "doc":
        case "dotm":
        case "xlsx":
        case "xlsm":
        case "xls":
        case "xltm":
        case "xlam":
        case "pptx":
        case "ppt":
        case "potx":
        case "potm":
        case "vsdx":
        case "vsdm":
            return "doc"
        case "png":
        case "jpg":
        case "jpeg":
        case "ico":
        case "gif":
        case "bmp":
        case "tiff":
            return "img"
        case "rar":
        case "tar":
        case "gzip":
        case "zip":
        case "jar":
        case "iso":
        case "ace":
        case "7-zip":
            return "zip"
        default:
            return ""
    }
}

function demand(e) {
    let userAry = consts.users()
    let projectAry = consts.projects()
    let me = auth.getAccount()
    
    if (!(e.p >= 0)) e.p = 2
    if (!e.status) e.status = 1
    
    if (e.deadline) e.deadlineStr = utils.timeStr(e.deadline, "Y-M-D")
    if (e.deadlineStr == '9999-09-09') {
        e.deadlineStr = ''
        e.deadline = ''
    }
    
    if (e.doneAt && (e.status == 100 || e.status == 110)) e.doneAtStr = utils.timeStr(e.doneAt, "Y-M-D")
    if (e.createAt) e.createAtStr = utils.timeStr(e.createAt, "Y-M-D")
    
    e.leaderName = ""
    for (let i = 0; i < userAry.length; i++) {
        let u = userAry[i]
        if (u.id == e.leader) {
            e.leaderName = u.name
            break
        }
    }
    
    e.users = []
    e.userNames = ""
    if (e.partners) {
        let ary = e.partners.split(",")
        for (let i = 0; i < ary.length; i++) {
            let u = parseInt(ary[i])
            if (!isNaN(u)) {
                e.users.push(u)
                
                for (let j = 0; j < userAry.length; j++) {
                    if (u == userAry[j].id) {
                        e.userNames += userAry[j].name + "、"
                        break
                    }
                }
            }
        }
    }
    if (e.userNames !== "") e.userNames = e.userNames.substring(0, e.userNames.length - 1);
    
    for (let j = 0; j < projectAry.length; j++) {
        let p = projectAry[j]
        if (p.id == e.projectId) {
            e.projectName = p.name
            break
        }
    }
    
    if (e.jobs) {
        for (let i = 0; i < e.jobs.length; i++) {
            let j = e.jobs[i]
            job(j)
        }
    }
    
    e.jobDesc = ""
    if (e.todoJob > 0) e.jobDesc = "待完成 " + e.todoJob + " 个"
    if (e.doneJob > 0 && e.todoJob == 0) e.jobDesc = "任务已全部完成"
    
    
    //初始化
    if (!e.id || !e.disabled) e.disabled = {status:{}}
    
    return e
}

function job(e) {
    let me = auth.getAccount()
    let userAry = consts.users()
    
    if (e.deadline) e.deadlineStr = utils.timeStr(e.deadline, "Y-M-D")
    if (e.deadlineStr == '9999-09-09') {
        e.deadlineStr = ''
        e.deadline = ''
    }
    
    if (e.createAt) e.createAtStr = utils.timeStr(e.createAt, "Y-M-D")
    if (e.doneAt && (e.status == 100 || e.status == 110)) e.doneAtStr = utils.timeStr(e.doneAt, "Y-M-D")
    
    if (e.leader) {
        e.leaderName = ""
        for (let i = 0; i < userAry.length; i++) {
            if (e.leader == userAry[i].id) {
                e.leaderName = userAry[i].name
                break
            }
        }
    }
    if (!e.id) e.editable = true
    if (me.roles.indexOf("1") >=0 || me.roles.indexOf("2") >= 0) e.editable = true
    e.done = e.status == 100 || e.status == 110
    return e
}

function chat(e) {
    let userAry = consts.users()
    if (e.createAt) e.createAtStr = utils.timeDesc(e.createAt)
    if (e.creator) {
        for (let i = 0; i < userAry.length; i++) {
            let u = userAry[i]
            if (u.id == e.creator) {
                e.creatorName = u.name
                break
            }
        }
    }
    return e
}


function bug(e) {
    let userAry = consts.users()
    
    if (e.deadline) e.deadlineStr = utils.timeStr(e.deadline, "Y-M-D")
    if (e.createAt) e.createAtStr = utils.timeStr(e.createAt, "Y-M-D")
    
    if (e.creator) {
        e.creatorName = ""
        for (let i = 0; i < userAry.length; i++) {
            if (e.creator == userAry[i].id) {
                e.creatorName = userAry[i].name
                break
            }
        }
    }
    
    e.userNames = ""
    e.users = []
    let ary = e.partners.split(",")
    for (let i = 0; i < ary.length; i++) {
        let u = parseInt(ary[i])
        if (!isNaN(u)) {
            e.users.push(u)
            
            for (let j = 0; j < userAry.length; j++) {
                if (userAry[j].id == u) {
                    e.userNames += userAry[j].name + "，"
                    break
                }
            }
        }
    }
    if (e.userNames !== "") e.userNames = e.userNames.substring(0, e.userNames.length - 1);
    
    if (!e.users) e.users = []
    console.log(e)
    return e
}
